<template>
    <LoginHeader>联合登录</LoginHeader>
    <!-- 切换按钮 -->
    <section class="container">
      <nav class="tab">
        <a
        @click="comName='callbackBind'"
        :class="{active:comName==='callbackBind'}"
          href="javascript:;"
        >
          <i class="iconfont icon-bind" />
          <span>已有小兔鲜账号，请绑定手机</span>
        </a>
        <a
        @click="comName='callbackPatch'"
        :class="{active:comName==='callbackPatch'}"
          href="javascript:;"
        >
          <i class="iconfont icon-edit" />
          <span>没有小兔鲜账号，请完善资料</span>
        </a>
      </nav>
      <!-- 显示的内容 -->
      <div class="tab-content">
        <!-- 动态组价，通过is属性绑定，绑定什么组价就渲染什么组件 -->
        <!-- <component :is="组件名"></component> -->
        <component :is="comName" :openId="openId"></component>
      </div>
    </section>
    <LoginFooter />
  </template>

<script>

import LoginHeader from './components/login-header'
import LoginFooter from './components/login-footer'
import callbackBind from './components/callback-bind.vue'
import callbackPatch from './components/callback-patch.vue'
import { getUserInfoAPI } from '@/api/login'
import { useStore } from 'vuex'
import { useRouter } from 'vue-router'
import QC from 'qc'
import { ref } from 'vue'
export default {
  name: 'PageCallback',
  components: {
    LoginHeader,
    LoginFooter,
    callbackBind,
    callbackPatch
  },
  setup () {
    const store = useStore()
    const router = useRouter()
    // 绑定组件初始值
    const comName = ref('callbackBind')
    // 保存openID
    const openId = ref('')
    // QC.Login.check():检测用户是否QQ成功，是则是true,否则false
    //  QC.Login.getMe():获取QQ分配openID,标识QQ登录成功的凭证
    if (QC.Login.check()) {
      // 如果QQ登录 认证成功
      // 获取QQ分配openID
      QC.Login.getMe(async openID => {
        // 保存openID
        openId.value = openID
        // 根据openID请求当前用户信息
        const res = await getUserInfoAPI(openID)
        // 如果绑定了,会得到用户信息对象
        // 否则未绑定
        if (res.code === 10086) {
          // 未绑定
          // 停留原地，请求用户绑定
        } else {
          // 已绑定
          // 通过vuex管理用户信息,更新用户信息
          store.commit('user/updateProfile', res.result)
          // 跳转到首页
          router.push('/')
        }
      })
    }

    return {
      comName,
      openId
    }
  }
}
</script>

  <style scoped lang='less'>
 .container {
  padding: 25px 0;
}
.tab {
  background: #fff;
  height: 80px;
  padding-top: 40px;
  font-size: 18px;
  text-align: center;
  a {
    color: #666;
    display: inline-block;
    width: 350px;
    line-height: 40px;
    border-bottom: 2px solid #e4e4e4;
    i {
      font-size: 22px;
      vertical-align: middle;
    }
    span {
      vertical-align: middle;
      margin-left: 4px;
    }
    &.active {
      color: @xtxColor;
      border-color: @xtxColor;
    }
  }
}
.tab-content {
  min-height: 600px;
  background: #fff;
}
  </style>
